package com.og.vas.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.og.vas.common.annotations.EnableLoginHandle;
import com.og.vas.common.exception.Assert;
import com.og.vas.common.exception.BusinessException;
import com.og.vas.common.result.R;
import com.og.vas.common.result.ResponseEnum;
import com.og.vas.pojo.dto.VacInfoDTO;
import com.og.vas.pojo.query.VacInfoQuery;
import com.og.vas.pojo.vo.VacInfoListVO;
import com.og.vas.service.VaccinaInfoService;
import com.og.vas.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xzy
 * @since 2022-01-11
 */
@RestController
@RequestMapping("/admin/vaccinaInfo")
@Api(tags = "接种信息相关操作接口")
public class VaccinaInfoController {

    @Autowired
    VaccinaInfoService service;
    //添加

    //按身份证号添加
    //按id添加
    //按用户名添加
    //按手机号添加

    @EnableLoginHandle(identity = 1)
    @ApiOperation(value = "接种信息添加(管理员权限)" ,notes = "type为添加方式（1.按身份证，2.按用户名，3.按手机号，4.按用户id）/t info为对应信息数据")
    @PostMapping("/add/{type}/{info}")
    public R add(@ApiParam("添加方式") @PathVariable Integer type,
                      @ApiParam(value = "用户信息",required = true) @PathVariable String info,
                      @ApiParam(value = "待添加数据",required = true) @RequestBody VacInfoDTO vacInfo) {

        //参数合法性校验
        Integer dose = vacInfo.getDose();
        Assert.isTrue(dose>0&&dose<=3, ResponseEnum.ABNORMAL_PARA);
       //类型判断调用
       switch (type){
           case 1:
               service.addByIdCardNum(info,vacInfo);
               break;
           case 2:
               service.addByUserName(info,vacInfo);
               break;
           case 3:
               service.addByPhone(info,vacInfo);
               break;
           case 4:
               service.addByUid(Integer.valueOf(info),vacInfo);
               break;
           default:
               throw new BusinessException("操作类型错误",-201);
       }



        return R.ok().message("添加成功");
    }


    //删除
    @EnableLoginHandle(identity = 1)
    @ApiOperation(value = "接种信息删除(管理员权限)" ,notes = "type为删除方式（1.按身份证，2.按用户名，3.按手机号，4.按用户id，5.表单id）/t info为对应信息数据")
    @DeleteMapping("/add/{type}/{info}")
    public R remove(@ApiParam(value = "删除方式",required = true) @PathVariable Integer type,
                      @ApiParam(value = "用户信息",required = true) @PathVariable String info) {
        //类型判断调用
        switch (type){
            case 1:
                service.deleteIdCardNum(info);
                break;
            case 2:
                service.deleteByUserName(info);
                break;
            case 3:
                service.deleteByPhone(info);
            case 4:
                service.deleteByUid(Integer.valueOf(info));
                break;
            case 5:
                service.deleteByid(Integer.valueOf(info));
                break;
            default:
                throw new BusinessException("操作类型错误",-201);

        }
        return R.ok().message("删除成功");
    }
    //查询
    //登录用户自查询
    @EnableLoginHandle
    @ApiOperation(value = "登录用户自查询接种信息(用户权限)" ,notes = "通过token获取数据，不需要携带其他参数")
    @GetMapping("/get")
    public R get(HttpServletRequest request) {
        return R.ok().message("查询成功").data("list",service.getByid(JwtUtils.getUserId(request.getHeader("token"))));
    }

    //管理员查询
    @EnableLoginHandle(identity = 1)
    @ApiOperation(value = "接种信息列表(管理员权限)" ,notes = "按照过滤条件展示列表，过滤条件可以为空")
    @GetMapping("/list/{page}/{limit}")
    public R list(@ApiParam(value = "当前页码", required = true) @PathVariable Long page,
                    @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit,
                    @ApiParam(value = "查询对象条件", required = false) VacInfoQuery query) {

        Page<VacInfoListVO> pages = new Page<>(page, limit);
        IPage<VacInfoListVO> list = service.list(pages, query);
        return R.ok().message("查询成功").data("page",list);
    }


    //修改
    @EnableLoginHandle(identity = 1)
    @ApiOperation(value = "修改接种信息(管理员权限)" ,notes = "按照过滤条件展示列表，过滤条件可以为空")
    @PutMapping("/update/{id}}")
    public R list(@ApiParam(value = "当前页码", required = true) @PathVariable Integer id,
                  @ApiParam(value = "待添加数据", required = true) @RequestBody VacInfoDTO vacInfo) {

        service.update(id,vacInfo);
        return R.ok().message("修改成功");
    }




}

